<template>
  <div class="app-container">
    <el-tree-select node-key="id" v-model="categoryId" :data="options2" :props="normalizer" :render-after-expand="false" />

    <el-button type="primary" @click="onEdit" v-if="!isEdit">编辑</el-button>
    <el-button type="primary" @click="onSave" v-else>保存</el-button>

    <el-card class="box-card mt">
      <!--表格渲染-->
      <el-table 
      v-loading="listLoading" 
      :data="list"
      border 
      fit 
      highlight-current-row
        style="width: 100%; margin-top: 10px" 
      >
  
        <el-table-column :show-overflow-tooltip="true" prop="title" label="名称">
          <template #default="scope">
            <span v-if="isEdit" >
            <el-input v-model="scope.row.title" placeholder="Please input" style="width: 20rem;" />
            </span>
            <span v-else>
              {{ scope.row.title }}
            </span>
          </template>
        </el-table-column>
       
        <el-table-column :show-overflow-tooltip="true" prop="seq" label="排序" width="80" >
        <template #default="scope">
          <el-input v-model="scope.row.seq" placeholder="Please input" v-if="isEdit" />
          <span v-else>
            {{ scope.row.seq }}
          </span>
        </template>
</el-table-column>
      </el-table>

    </el-card>
  </div>
</template>

<script>
import { getAllCategory, GetExaminationByCategoryId, saveExamination } from "@/api/zhongyi/examination";


export default {
  name: "examination",
  data() {
    return {
      list: null,
      listLoading: true,
      isEdit: false,
      normalizer: {
          label: 'name',
          children: 'child',
      },
      options2: [],
      categoryId: ''
    };
  },
  computed: {
    getType() {
      return function (val) {
        return this.categoryOptions.find(t => t.value == val).label;
      };
    },
  },
  created() {
    this.listLoading = true;
    getAllCategory().then((res) => {
        this.options2 = res.result;
        // this.total = res.result.total;
        this.listLoading = false;
      });
  },
  methods: {
    getList() {
      this.listLoading = true;
      GetExaminationByCategoryId(this.categoryId).then((res) => {
        this.list = res.result;
        // this.total = res.result.total;
        this.listLoading = false;
      });
    },

    onEdit() {
      this.isEdit = true;
    },
    onSave() {
      this.listLoading = true;
      saveExamination(this.list).then(_=> {
        this.isEdit = false;
        //this.listLoading = false;

        this.getList();
      })
      
    },

  },
  mounted: function () { },
  watch: {
    categoryId: {
      handler(val, oldVal) {
        this.getList();
      },
      deep: true
    },
  }
};
</script>


